package com.tanhua.db.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.api.UserInfoApi;
import com.tanhua.db.mapper.UserInfoMapper;
import com.tanhua.model.dto.RecommendUserDto;
import com.tanhua.model.mongo.RecommendUser;
import com.tanhua.model.pojo.UserInfo;
import com.tanhua.model.vo.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class UserInfoApiImpl implements UserInfoApi {
    @Autowired
    UserInfoMapper userInfoMapper;

    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);

    }

    @Override
    public void updateById(UserInfo userInfo) {
userInfoMapper.updateById(userInfo);
    }

    @Override
    public UserInfo selectById(Long id) {
        UserInfo userInfo = userInfoMapper.selectById(id);
        return userInfo;
    }

    @Override
    public Page<UserInfo> page(Page<UserInfo> page, Long userId) {
        Page<UserInfo> userInfoPage = userInfoMapper.pageByblack(page, userId);

        return userInfoPage;
    }

    @Override
    public Page<UserInfo> findUsersByIds(List<RecommendUser> recommendUserList, RecommendUserDto recommendUserDto) {
        List<Long> ids = recommendUserList.stream().map(recommendUser -> {
            return recommendUser.getUserId();
        }).collect(Collectors.toList());

        LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(UserInfo::getId,ids).
                lt(recommendUserDto.getAge() != null, UserInfo::getAge, recommendUserDto.getAge()).eq(StringUtils.isNotBlank(recommendUserDto.getGender()), UserInfo::getGender, recommendUserDto.getGender());
        Page<UserInfo> page = new Page<>(recommendUserDto.getPage(), recommendUserDto.getPagesize());
        Page<UserInfo> userInfoPage = userInfoMapper.selectPage(page, wrapper);
        return userInfoPage;
    }
//根据用户id集合查询用户详细信息
    @Override
    public List<UserInfo> findUserinfoByIds(List<Long> ids) {
        List<UserInfo> userInfoList = userInfoMapper.selectList(Wrappers.lambdaUpdate(UserInfo.class).in(UserInfo::getId,ids));
        return userInfoList;
    }
//根据用户id查询单个用户具体信息
    @Override
    public UserInfo findByid(Long userId) {
        UserInfo userInfo = userInfoMapper.selectById(userId);
        return userInfo;
    }
//根据id查询好友列表
    @Override
    public PageResult<UserInfo> selectPage(List<Long> friends, Integer page, Integer pagesize, String keyword) {
        LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(keyword),UserInfo::getNickname,keyword)
                .in(UserInfo::getId,friends);
        Page<UserInfo> page1 = new Page<>(page, pagesize);
        Page<UserInfo> userInfoPage = userInfoMapper.selectPage(page1, wrapper);
        PageResult<UserInfo> pageResult = new PageResult<>(page,pagesize,0,page1.getRecords());

        return pageResult;
    }

    @Override
    public List<UserInfo> findByUserIds(List<Long> userIds, String gender) {
        return userInfoMapper.selectList(Wrappers.lambdaQuery(UserInfo.class)
                .in(CollUtil.isNotEmpty(userIds), UserInfo::getId, userIds)
                .eq(StringUtils.isNotBlank(gender), UserInfo::getGender, gender));
    }

    @Override
    public Page<UserInfo> findByPage(Integer page, Integer pagesize) {
        return userInfoMapper.selectPage(new Page<>(page,pagesize),null);
    }
}
